<template>
  <el-card>
    <div style="display: flex; justify-content: space-between; margin-bottom: 20px;">
      <el-button type="primary" @click="handleAdd">新增成果</el-button>
    </div>

    <el-table v-loading="loading" :data="researchList" style="width: 100%">
      <el-table-column prop="type" label="成果类型" width="120" />
      <el-table-column prop="name" label="成果名称" width="180" />
      <el-table-column prop="journal" label="发表期刊" />
      <el-table-column prop="inclusion" label="收录情况" width="120" />
      <el-table-column prop="awards" label="获奖情况" width="120" />
      <el-table-column prop="date" label="发表时间" width="120" />
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        v-if="total > 0"
        :current-page="pageNum"
        :page-size="pageSize"
        :total="total"
        layout="prev, pager, next"
        style="margin-top: 20px;"
        @current-change="handlePageChange"
    />
  </el-card>

  <el-dialog v-model="dialogVisible" :title="dialogTitle" width="50%">
    <el-form :model="formData" label-width="100px">
      <el-form-item label="成果类型">
        <el-input v-model="formData.type" placeholder="请输入成果类型"/>
      </el-form-item>
      <el-form-item label="成果名称">
        <el-input v-model="formData.name" placeholder="请输入成果名称"/>
      </el-form-item>
      <el-form-item label="发表期刊">
        <el-input v-model="formData.journal" placeholder="请输入发表期刊"/>
      </el-form-item>
      <el-form-item label="收录情况">
        <el-input v-model="formData.inclusion" placeholder="请输入收录情况"/>
      </el-form-item>
      <el-form-item label="获奖情况">
        <el-input v-model="formData.awards" placeholder="请输入获奖情况"/>
      </el-form-item>
      <el-form-item label="发表时间">
        <el-date-picker 
          v-model="formData.date" 
          type="date" 
          placeholder="选择日期" 
          style="width: 100%"
          value-format="YYYY-MM-DD"
          format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="成果描述">
        <el-input v-model="formData.description" type="textarea" rows="4" placeholder="请输入成果描述"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="submitForm">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import {onMounted, ref} from 'vue'
import {ElMessage, ElMessageBox} from 'element-plus'
import  {idel, iget, ipost} from '@/api'
import store from "@/store";

export default {
  setup() {
    const loading = ref(false)
    const researchList = ref([])
    const pageNum = ref(1)
    const pageSize = ref(10)
    const total = ref(0)
    const dialogVisible = ref(false)
    const dialogTitle = ref('')
    const formData = ref({
      researchId: null,
      id: null,
      type: '',
      name: '',
      journal: '',
      inclusion: '',
      awards: '',
      date: '',
      description: ''
    })
    const currentId = ref(null)

    const fetchResearchList = async () => {
      loading.value = true
      try {
        const res = await iget('/research/listById/'+store.state.id, null, {
          params: {
            pageNum: pageNum.value,
            pageSize: pageSize.value
          }
        })
        researchList.value = res.data.result.records
        total.value = res.data.result.total
      } catch (error) {
        ElMessage.error('获取数据失败')
      } finally {
        loading.value = false
      }
    }

    const handlePageChange = (val) => {
      pageNum.value = val
      fetchResearchList()
    }

    const handleAdd = () => {
      dialogTitle.value = '新增科研成果'
      formData.value = {
        id: store.state.id,
        type: '',
        name: '',
        journal: '',
        inclusion: '',
        awards: '',
        date: '',
        description: ''
      }
      currentId.value = null
      dialogVisible.value = true
    }

    const handleEdit = (index, row) => {
      console.log(index)
      dialogTitle.value = '修改科研成果'
      formData.value = {...row}
      currentId.value = row.id
      dialogVisible.value = true
    }

    const handleDelete = (index, row) => {
      console.log(index)
      ElMessageBox.confirm(`确定删除 "${row.name}"?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await idel('/research/delete', {params: {id: row.researchId}})
          ElMessage.success('删除成功')
          await fetchResearchList()
        } catch (error) {
          ElMessage.error('删除失败')
        }
      }).catch(() => {
      })
    }

    const submitForm = async () => {
      try {
        if (currentId.value) {
          await ipost('/research/update', formData.value)
          ElMessage.success('修改成功')
        } else {
          await ipost('/research/add', formData.value)
          ElMessage.success('添加成功')
        }
        dialogVisible.value = false
        await fetchResearchList()
      } catch (error) {
        ElMessage.error('操作失败')
      }
    }

    onMounted(() => {
      fetchResearchList()
    })

    return {
      loading,
      researchList,
      pageNum,
      pageSize,
      total,
      dialogVisible,
      dialogTitle,
      formData,
      handlePageChange,
      handleAdd,
      handleEdit,
      handleDelete,
      submitForm
    }
  }
}
</script>